package com.sskj.gocoinapp.ui.activity;


import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gocoinapp.R;
import com.sskj.gocoinapp.bean.rxbus.AccountBean;
import com.sskj.gocoinapp.presenter.RegisterNextActivityPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;

@Route(path = RConfig.APP_REGISTER_NEXT)
public class RegisterNextActivity extends BaseActivity<RegisterNextActivityPresenter> {
    @Autowired
    String code;
    @Autowired
    String account;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R.id.etInviteCode)
    EditText etInviteCode;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.tvAgreement)
    TextView tvAgreement;
    @BindView(R.id.btLogin)
    Button btLogin;
    private boolean isSeeAgain;
    private boolean isSee;
    private AndroidNextInputs inputs;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_register_next;
    }

    @Override
    public RegisterNextActivityPresenter getPresenter() {
        return new RegisterNextActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity1));
        ARouter.getInstance().inject(this);
        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            isSeeAgain = !isSeeAgain;
            etPwdAgain.setTransformationMethod(isSeeAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggleAgain.setImageResource(isSeeAgain ? R.mipmap.app_icon_show : R.mipmap.app_icon_hide);
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.app_icon_show : R.mipmap.app_icon_hide);

        });
        ClickUtil.click(tvAgreement, () -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constans.INTENT_NOTICE_ID, "reg_agree");
            ARouter.getInstance().build(RConfig.MINE_WEBVIEW).with(bundle).navigation();
        });

        inputs = new AndroidNextInputs();
        inputs.clear();
        inputs
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank())
                .add(etInviteCode, StaticScheme.NotBlank());
        ClickUtil.click(btLogin, () -> {
            if (inputs.test()) {
                if (!checkBox.isChecked()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity2));
                    return;
                }
                mPresenter.register(account, code,
                        etPwd.getText().toString(),
                        etPwdAgain.getText().toString(),
                        etInviteCode.getText().toString()
                );
            }
        });
    }

    public void registerSuccess() {
        RxBus.getDefault().post(new AccountBean(account));
        finish();
    }

}
